﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Text;

namespace RenameTracker
{
    public class ChangeInfoExplained : ChangeInfo
    {        
        public ChangeInfoExplained(Changeset c)
            : this(new ChangeInfo(c))
        { }

        public ChangeInfoExplained(ChangeInfo ci)
            : base(ci)
        {
            Explain();
        }

        // calculated properties
        public IEnumerable<NameChangeComponent> NameChangeComponents 
        {
            get; private set;
        }
        public string Explanation
        {
            get; private set;
        }

        public override string ToString()
        {
            return string.Format("{0}\nExplanation:\n{1}", base.ToString(), Explanation);
        }

        private void Explain()
        {
            NameChangeComponents = Tracker.CategorizeNameChangeComponents(this);
                        
            string simpleExplanation = PendingChange.GetLocalizedStringForChangeType(ChangeType);
            if (NameChangeComponents == null)
            {
                Explanation = simpleExplanation;            
                return;
            }

            var sb = new StringBuilder(simpleExplanation);
            sb.AppendLine();
            foreach (var nameChange in NameChangeComponents)
            {
                sb.AppendLine(nameChange.ToString());
            }
            Explanation = sb.ToString();
        }        
    }
}
